package cloud.maque.auth.handler.oauth2callback;

import cloud.maque.auth.model.Oauth2AccessTokenRes;
import cloud.maque.auth.model.Oauth2User;
import cloud.maque.auth.service.Oauth2CallBackService;
import org.springframework.stereotype.Service;

@Service
public class DefaultOauth2CallBackHandler<T> extends AbstractOAuth2ProcessorHolder<Oauth2CallBackService> implements Oauth2CallBackInterface {


    /**
     * 获取AccessToken
     *
     * @param code
     * @return
     */
    @Override
    public Oauth2AccessTokenRes getAccessToken(String code, String loginType) {
        Oauth2CallBackService processor = getProcessor(Oauth2CallBackService.class, loginType);
        if (processor == null) {
            throw new RuntimeException("未找到对应第三方登录处理器:" + loginType);
        }
        return processor.getAccessToken(code);
    }

    /**
     * 获取第三方的用户信息
     *
     * @param accessToken
     * @return
     */
    @Override
    public Oauth2User getUser(String accessToken, String loginType) {
        Oauth2CallBackService processor = getProcessor(Oauth2CallBackService.class, loginType);
        if (processor == null) {
            throw new RuntimeException("未找到对应第三方登录处理器:" + loginType);
        }
        return processor.getUser(accessToken);
    }
}
